package cn.lyd.system.service.impl;

import cn.lyd.base.service.impl.BaseServiceImpl;
import cn.lyd.system.domain.Dictionarydetail;
import cn.lyd.system.domain.Dictionarytype;
import cn.lyd.system.mapper.DictionarydetailMapper;
import cn.lyd.system.service.IDictionarytypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DictionarytypeServiceImpl extends BaseServiceImpl<Dictionarytype> implements IDictionarytypeService {


    @Autowired
    private DictionarydetailMapper dictionarydetailMapper;

    /**
     * 级联删除
     * @param id
     */
    @Override
    public void delete(Long id) {
        //点击删除，获取当前行的id
        //根据id去查detail表中的type_id 如果相等，调用detail的删除方法删掉该type_id

        //拿到所有detail数据的集合
        List<Dictionarydetail> details = dictionarydetailMapper.findAll();
        //遍历数据
        for (Dictionarydetail dictionarydetail: details) {
            //如果detail表中的type_id=前端传来的id【即type表的id】
            if (dictionarydetail.getTypes_id().equals(id)){
                 //该数据就是要级联删除的数据,获取它的id
                Long delDetailId = dictionarydetail.getId();
                //干掉它
                dictionarydetailMapper.delete(delDetailId);
            }
        }
        //删完了明细表的数据，最后调用父方法，干掉这条type数据
        super.delete(id);

    }

    /**
     * 级联批量删除-双重for
     * @param ids
     */
    @Override
    public void batchDelete(Long[] ids) {
        //拿到所有detail数据的集合
        List<Dictionarydetail> details = dictionarydetailMapper.findAll();
        //遍历数据
        for (Dictionarydetail dictionarydetail: details) {
            //遍历取出前端传来的每一个id
            for (Long id:ids){
                //如果detail表中的type_id=前端传来的id【即type表的id】
                if (dictionarydetail.getTypes_id().equals(id)) {
                    //该数据就是要级联删除的数据,获取它的id
                    Long delDetailId = dictionarydetail.getId();
                    //干掉它
                    dictionarydetailMapper.delete(delDetailId);
                }
            }
        }
        //删完了明细表的数据，最后调用父方法，干掉这组type数据
        super.batchDelete(ids);
    }


}
